﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
    public class Zor_NaturalPerson_Bll<T> where T : class
    {
        #region - 自然人管理 -

        /// <summary>
        /// 获取自然人管理列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model,int index, out int TotalCount, string where)
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id; //获取当前登录人的部门ID
            if (Model.Tenant_Name == null &&
             Model.Lease_StartTime == null &&
             Model.NatPer_Name == null &&
             Model.NatPer_Phone == null &&
             Model.NatPer_IDNumber == null &&
             Model.Acceptance_TicketNumber == null &&
             Model.Acceptance_TaxNumber == null &&
             Model.State == null &&
             Model.AcceptanceType == null
             )
            {
                var sqlSurch = " Dt_Id in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + ") and Acceptance_Type=2 and State in('已保存','被退回') ";
                return PagerHelp<T>.GetPagerList(index, "V_Acceptance", "Acceptance_CreateTime desc", out TotalCount, sqlSurch);
            }
            return PagerHelp<T>.GetPagerList(index, "V_Acceptance", "Acceptance_CreateTime desc ", out TotalCount, GetSearchWhere(Model, "Dt_Id in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + ") and Acceptance_Type=2"));
        } 
     
        /// <summary>
        /// 获取自然人管理列表（不分页）
        /// create zhl by 2017.3.28     
        /// </summary>
        /// <returns></returns>
        public static List<T> GetList(dynamic Model, string where)
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id; //获取当前登录人的部门ID

            if (Model.Tenant_Name == null &&
             Model.Lease_StartTime == null &&
             Model.NatPer_Name == null &&
             Model.NatPer_Phone == null &&
             Model.NatPer_IDNumber == null &&
             Model.Acceptance_TicketNumber == null &&
             Model.Acceptance_TaxNumber == null &&
             Model.State == null &&
             Model.AcceptanceType == null
             )
            {
                where = "Dt_Id in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + ") and Acceptance_Type=2 and State in('已保存','被退回') ";
                return Zor_NaturalPerson_DAL<T>.SelectViewAcceptance(where);
            }
            else
            {
                where = "Dt_Id in (select Dt_Id  from dbo.Sys_Department where Dt_ParentId=" + Dtid + ") and Acceptance_Type=2";
                return   Zor_NaturalPerson_DAL<T>.SelectViewAcceptance(GetSearchWhere(Model, where));
            }
                   
        }

        /// <summary>
        /// 查询当前街道(根据当前部门ID)
        /// create zhl by 2017.3.29    
        /// </summary>      
        /// <param name="dt_Id">根据社区ID</param>
        /// <returns></returns>
        public static List<T> SelectStreetById()
        {
            return Zor_NaturalPerson_DAL<T>.SelectStreetById(RequestSession.GetSessionUser().DT_Id);        
        }
        /// <summary>
        /// 查询所有街道(根据社区ID)
        /// create zhl by 2017.3.29    
        /// </summary>      
        /// <param name="dt_Id">根据社区ID</param>
        /// <returns></returns>
        public static T SelectStreet(int dt_Id)
        {
            return Zor_NaturalPerson_DAL<T>.SelectStreet(dt_Id);
        }
     
        /// <summary>
        /// 查询所有社区
        /// create zhl by 2017.3.28
        /// <param name="parentid">街道ID</param>
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectCommunity(int dt_Id)
        {
            return Zor_NaturalPerson_DAL<T>.SelectCommunity(dt_Id);
        }

        /// <summary>
        /// 自然人管理-新增
        /// create zhl by 17-3-30
        /// </summary>
        /// <param name="naturalPerson">自然人实体</param>
        /// <param name="lease">租赁实体</param>
        /// <param name="acceptance">受理单实体</param>
        /// <param name="tenant">租户实体</param>
        /// <param name="house">房屋实体</param>
        /// <param name="unitInfo">单元实体</param>
        /// <param name="Build">楼宇实体</param>
        /// <param name="leasestartTime">租赁开始时间</param>
        /// <param name="leaseendTime">租赁结束时间</param>
        /// <param name="taxstarttime">税金开始时间</param>
        /// <param name="taxendtime">税金结束时间</param>
        /// <returns> Succee = 1//成功 Failure = 2, //失败Error = 3 //异常</returns>
        public static BaseAnswer InsertNaturalPerson(Zor_NaturalPerson naturalPerson, Zor_Lease lease, Zor_Acceptance acceptance, Zor_Tenant tenant, Zor_Hous house, Zor_UnitInfor unitInfo, Zor_Building Build, DateTime leasestartTime, DateTime leaseendTime, DateTime taxstarttime, DateTime taxendtime)
        {
            naturalPerson.NatPer_CreateTime = TimeHelper.ToTimeStamp(DateTime.Now);
            naturalPerson.NatPer_CreateName = RequestSession.GetSessionUser().User_Id;
            lease.Lease_StartTime = TimeHelper.ToTimeStamp(leasestartTime).ToString();
            lease.Lease_EndTime = TimeHelper.ToTimeStamp(leaseendTime).ToString();
            acceptance.Acceptance_TaxStartTime = TimeHelper.ToTimeStamp(taxstarttime);
            acceptance.Acceptance_TaxEndTime = TimeHelper.ToTimeStamp(taxendtime);
            lease.Lease_Rent = lease.Lease_Rent * 100;
            BaseAnswer bar = Zor_NaturalPerson_DAL<T>.InsertNaturalPerson(naturalPerson, lease, acceptance, tenant, house, unitInfo, Build);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "增加成功！" : "增加失败！";
            return bar;
        }

        /// <summary>
        /// 自然人管理-修改
        /// create zhl by 17-3-30
        /// </summary>
        /// <param name="naturalPerson">自然人实体</param>
        /// <param name="lease">租赁实体</param>
        /// <param name="acceptance">受理单实体</param>
        /// <param name="tenant">租户实体</param>
        /// <param name="house">房屋实体</param>
        /// <param name="unitInfo">单元实体</param>
        /// <param name="Build">楼宇实体</param>
        /// <returns> Succee = 1//成功 Failure = 2, //失败Error = 3 //异常</returns>
        public static BaseAnswer UpdateNaturalPerson(Zor_NaturalPerson naturalPerson, Zor_Lease lease, Zor_Acceptance acceptance, Zor_Tenant tenant, Zor_Hous house, Zor_UnitInfor unitInfo, Zor_Building Build, DateTime leasestartTime, DateTime leaseendTime, DateTime taxstarttime, DateTime taxendtime)
        {
            naturalPerson.NatPer_ModifyDate = TimeHelper.ToTimeStamp(DateTime.Now);
            naturalPerson.NatPer_ModifyName = RequestSession.GetSessionUser().User_Id;
            lease.Lease_StartTime = TimeHelper.ToTimeStamp(leasestartTime).ToString();
            lease.Lease_EndTime = TimeHelper.ToTimeStamp(leaseendTime).ToString();
            acceptance.Acceptance_TaxStartTime = TimeHelper.ToTimeStamp(taxstarttime);
            acceptance.Acceptance_TaxEndTime = TimeHelper.ToTimeStamp(taxendtime);
            lease.Lease_Rent = lease.Lease_Rent * 100;
            BaseAnswer bar = Zor_NaturalPerson_DAL<T>.UpdateNaturalPerson(naturalPerson, lease, acceptance, tenant, house, unitInfo, Build);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "修改成功！" : "修改失败,请检查租户是否已存在！";
            return bar;
        }

        /// <summary>
        /// 删除自然人
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="id">受理单id</param>
        /// <returns> Succee = 1//成功 Failure = 2, //失败Error = 3 //异常</returns>
        public static BaseAnswer DeleteNaturalPerson(int Acceptance_Id, int Lease_Id, int Hs_Id, int Uim_Id, int Bd_Id, int Hs_attributes)
        {
            BaseAnswer bar = Zor_NaturalPerson_DAL<T>.DeleteNaturalPerson(Acceptance_Id, Lease_Id, Hs_Id, Uim_Id, Bd_Id, Hs_attributes);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "删除成功！" : "删除失败！当前受理单已提交至评议";
            return bar;
        }
        /// <summary>
        /// 推送
        /// create zhl by 17.3.30
        /// </summary>
        /// <param name="id">受理单id</param>
        /// <returns>2--当前受理单已推送-1操作失败1--操作成功的标识</returns> 
        public static string Push(int id, int acceptancestate, int dtid)
        {  
            var  messge="";
            int narid = Zor_NaturalPerson_DAL<T>.Push(id, acceptancestate, dtid);
            if (narid == 1)
            { 
               messge="推送成功";
            }
            else if (narid == 2)
            {
                messge = "当前受理单已推送，不能重复推送";
            }
            else {
                messge = "推送失败";
            }
            return messge;
        }
        /// <summary>
        /// 社区受理单推送
        /// create zhl by 17.3.30
        /// </summary>
        /// <param name="id">受理单id</param>
        /// <returns>2--当前受理单已推送-1操作失败1--操作成功的标识</returns> 
        public static string AccPush(int acceptanceid ,int acceptancestate)
        {
            var messge = "";
            int narid = Zor_NaturalPerson_DAL<T>.AccPush(acceptanceid, acceptancestate);
            if (narid == 1)
            {
                messge = "推送成功";
            }
            else if (narid == 2)
            {
                messge = "当前受理单已推送，不能重复推送";
            }
            else
            {
                messge = "推送失败";
            }
            return messge;
         
        }
        
        /// <summary>
        /// 查询地址
        /// </summary>
        /// <param name="dt_Id"></param>
        /// <returns></returns>
        public static List<T> GetAcceptanceAddress(string NatPer_Name, string Tenant_Name)
        {
            return Zor_NaturalPerson_DAL<T>.GetAcceptanceAddress(NatPer_Name, Tenant_Name);
        }

        /// <summary>
        /// 推送并评议
        /// create zhl by 17.3.30
        /// </summary>
        /// <param name="ApeId">受理单id</param>
        /// <param name="propertytax">评议房屋税金</param>
        /// <param name="tax">评议税金</param>
        /// <param name="totaltax">评议计税总额</param>
        /// <param name="personaltax">评议个税</param>
        /// <param name="rentMoney">评议租金</param>
        /// <param name="comment"></param>
        /// <returns>2--当前受理单已推送-1操作失败1--操作成功的标识</returns> 
        public static string PushReview(int ApeId, int propertytax, int tax, int totaltax, int personaltax, int rentMoney, string comment, int acceptancestate, int dtid)
        {
      
            var  messge="";
            int narid = Zor_NaturalPerson_DAL<T>.PushReview(ApeId, propertytax, tax, totaltax, personaltax, rentMoney, comment, acceptancestate, dtid);
            if (narid == 1)
            { 
               messge="推送并评议成功";
            }
            else if (narid == 2)
            {
                messge = "当前受理单已推送评议，不能重复推送评议";
            }
            else {
                messge = "推送评议失败";
            }
            return messge;
        }

        #region 私有函数

        private static string GetSearchWhere(dynamic Model, string where)
        {
            var sqlSurch = string.Empty;            
      
            if (Model.Tenant_Name != null)
            {
                sqlSurch += " and Tenant_Name like '%" + Model.Tenant_Name + "%'";
            }
            if (Model.Lease_StartTime != null && Model.Lease_EndTime != null)
            {
                sqlSurch += " and Lease_StartTime >="+TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_StartTime.ToString()))+" and Lease_EndTime<="+TimeHelper.ToTimeStamp(DateTime.Parse(Model.Lease_EndTime.ToString()))+"";
            }
            if (Model.NatPer_Name != null)
            {
                sqlSurch += " and NatPer_Name like '%" + Model.NatPer_Name + "%'";
            }
            if (Model.NatPer_Phone != null)
            {
                sqlSurch += " and NatPer_Phone = '" + Model.istaxregistration + "'";
            }
            if (Model.NatPer_IDNumber != null)
            {
                sqlSurch += " and NatPer_IDNumber = '" + Model.NatPer_IDNumber + "'";
            }
            if (Model.Acceptance_TicketNumber != null)
            {
                sqlSurch += " and Acceptance_TicketNumber = '" + Model.Acceptance_TicketNumber + "'";
            }
            if (Model.State != null)
            {
                if (Model.State == "全部")
                {
                    sqlSurch += " and State in ('已保存','已提交','被退回','已评议','已评定','已审核','已缴税','已开票','已销号','已汇总')";
                }
                else
                {
                    sqlSurch += " and State = '" + Model.State + "'";
                }

            }
            if (Model.AcceptanceType != null)
            {
                if (Model.AcceptanceType == "全部")
                {
                    sqlSurch += " and AcceptanceType in('社区','自然人')";
                }
                else
                {
                    sqlSurch += " and AcceptanceType = '" + Model.AcceptanceType + "' ";
                }
            }
            return where + sqlSurch;
        }
        #endregion
        #endregion 
      
    }
}
